<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mod_email extends CI_Controller {
    private $per_page = 1;
    private $total_rows = 12;
    private $uri_page ='';
    private $base_page = '';
    private $uri_segment='';
    function  __construct(){
        parent::__construct();
    }
    function index(){
        if($this->session->userdata('id')){
			$dat['activeden']=1;
			$dat['email2']=$this->common_model->getone('email',array('email_to'=>$this->session->userdata('email'),'publish'=>1),'',array('date','DESC'));
			if($dat['email2']){
				if($dat['email2']->email_to == $this->session->userdata('email') || $dat['email2']->email_from == $this->session->userdata('email')){
					$dat['emailgui']=$this->common_model->getdata('email',array('email_from'=>$dat['email2']->email_from,'email_to'=>$dat['email2']->email_to,'publish'=>1),'',array('id','DESC'));
					$dat['emailnhan']=$this->common_model->getdata('email',array('email_from'=>$dat['email2']->email_to,'email_to'=>$dat['email2']->email_from,'publish'=>1),'',array('id','DESC'));
					$dat['user']=$this->common_model->getone('users',array('email'=>$dat['email2']->email_from));
					if(!empty($dat['emailnhan'])){
					   $dat["mangnoi"] = array_merge($dat['emailgui'],$dat['emailnhan']); 
					}
					else{
						$dat["mangnoi"] = $dat['emailgui'];
					}
					arsort($dat["mangnoi"]);
					$this->common_model->updatedata('email',array('id'=>$dat['email2']->id),array('status'=>1));
					if($this->input->post('mes_traloi') != ''){
						$mess = $this->input->post('mes_traloi');
						$mail_from = $this->session->userdata('email');
						$mail_to = $dat['email2']->email_from;
						$this->common_model->insertdata('email',array('email_from'=>$mail_from,'email_to'=>$mail_to,'subjects'=>$dat['email2']->subjects,'description'=>$mess));
						$dat['thongbao']='<p style="color:green;"><i class="icon-check-sign icon-2x"></i>&nbsp;&nbsp;Bạn đã gửi email thành công. Ban quản trị sẽ duyệt trong vòng 24h</p>';
					}
					$data['content']=$this->load->view('mod_email/chitiet',$dat,true);
				}
				else{
					$data['content']='<div class="wrapper clearfix min720"><h1>Bạn không có quyền xem email này!</h1></div>';
				}
			}
			else{
				$data['content']='<div class="wrapper clearfix min720"><h1>Bạn không có quyền xem email này!</h1></div>';
			}
        }
        $offset=$this->uri->segment(3);
        $url=$this->uri->segment(1).'/'.$this->uri->segment(2).'.html';
        $thongbao='Bạn cần đăng nhập để sử dụng chức năng này';
        if($this->session->userdata('id')){
			$this->db->order_by('status','ASC');
            $dat['email2']=$this->common_model->getdata('email',array('publish'=>1,'email_to'=>$this->session->userdata('email')),'',array('date','DESC'));
			
            if(!empty($dat['email2']))
            {
                foreach($dat['email2'] as $m)
                {
                    $mang1[] = $m->email_from;
                }
                $mang2 = array_unique($mang1);
                foreach ($mang2 as $mang3)
                {
                    $this->db->order_by('id','DESC');
                    $dat['email'][]=$this->common_model->getone('email',array('publish'=>1,'email_to'=>$this->session->userdata('email'),'email_from'=>$mang3),'',array('date','DESC'),'',array('email_from'=>$this->session->userdata('email'),'email_to'=>$mang3));
                }
            }
            $this->total_rows=$this->common_model->getnumber2('email',array('publish'=>1,'email_to'=>$this->session->userdata('email')),'','','',array('email_from'=>$this->session->userdata('email')));
            $this->phantrang();
            $data['content']=$this->load->view('mod_email/views',$dat,true);
        }else{
            $url = base_url().'email/chi-tiet/'.$offset.'.html';
            $data['content']=$this->load->view('mod_account/views_login',array('url'=>$url,'thongbao'=>$thongbao),true);
        }
        $this->load->view('index',$data);
    }
    function gopy(){ 
        $thongbao='';
        $this->load->library('form_validation');
        $this->form_validation->set_rules('phone', 'Điện thoại', 'numeric|min_length[10]|max_length[15]|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|xss_clean');
        $this->form_validation->set_rules('content', 'Nội dung', 'required|xss_clean');
        $this->form_validation->set_error_delimiters('<span class="error3">', '</span>');
        if(!empty($_COOKIE['gopy']))
        {   
            if ($this->form_validation->run() == FALSE){

            }
            else{
                $email=$this->input->post('email');
                $phone=$this->input->post('phone');
                $content=$this->input->post('content');
                date_default_timezone_set('GMT');
                $this->load->library('email');
                $this->email->set_newline("\r\n"); 
                $config['mailtype']='html';
                $config['proxy_ips'] = '';
                $config['protocol'] = 'smtp';
                $config['smtp_host']='ssl://smtp.googlemail.com';
                $config['smtp_user']='info.10cong@gmail.com';
                $config['smtp_pass']='10cong.com';
                $config['smtp_port']='465';
                $this->email->initialize($config);
                $this->email->from('info.10cong@gmail.com','10 Cộng');
                $this->email->to('info.10cong@gmail.com');   
                $this->email->subject('Email góp ý về 10cong.com '); 
                $this->email->message('
                    Người góp ý : '.$email.'<br />Số điện thoại: '.$phone.'<br />Nội dung:<br />'.$content.'
                ');  
                $this->email->send();
                $thongbao = '<p style="font-weight:bold;font-size:14px;color:#fff;padding-left:20px">Chúng tôi đã nhận được thư góp ý của bạn. Chúng tôi sẽ trả lời thư trong thời gian sớm nhất. Xin chân thành cảm ơn<script>                setTimeout(function(){
                     $("#contact-pp").slideUp(400);
                     $(".overlay").remove();
                },3000)</script>' ;
                setcookie("gopy","OK",time()+300);
            }
        }
        else
        {
            $thongbao = '<p style="font-weight:bold;font-size:14px;color:#fff;padding-left:20px">Bạn đã gửi góp ý cách đây không lâu</p><script>                setTimeout(function(){
                     $("#contact-pp").slideUp(400);
                     $(".overlay").remove();
                },3000)</script>';
            setcookie("gopy","OK", time()+300);
            
        }
        $this->load->view('mod_email/form_contact',array('thongbao'=>$thongbao));
    }
    function soanmail(){
        $id=$this->uri->segment(3);
        $url=$this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$id;
        $thongbao='Bạn cần đăng nhập để sử dụng chức năng này';
        if($this->session->userdata('id')){
            if(!empty($id)){
                $dat['mail']=$this->common_model->getone('email',array('id'=>$id));
            }else{
                $dat['mail']='';
            }
            $dat['user']=$this->common_model->getone('users',array('email'=>$dat['mail']->email_from));
            if($this->input->post('ok')){
                $email_from=$dat['mail']->email_from;
                $subjects=$this->input->post('subjects');
                $noidung=$this->input->post('noidung');
                $this->common_model->insertdata('email',array('email_to'=>$email_from,'email_from'=>$dat['mail']->email_to,'subjects'=>$subjects,'description'=>$noidung));
                $dat['thongbao']='<span style="color:green;font-weight:bold"><i class="icon-check 1x"></i> Đã gửi mail thành công;</span>';
            }
            $data['content']=$this->load->view('soanmail',$dat,true);
        }else{
            $data['content']=$this->load->view('mod_account/views_login',array('url'=>$url,'thongbao'=>$thongbao),true);
        }
        $this->load->view('index',$data);
    }
    
    function thuden(){
        $offset=$this->uri->segment(3);
        $url=$this->uri->segment(1).'/'.$this->uri->segment(2).'.html';
		$thongbao='Bạn cần đăng nhập để sử dụng chức năng này';
		if($this->session->userdata('id')){
            $dat['activeden']=1;
            $dat['email2']=$this->common_model->getdata('email',array('email_to'=>$this->session->userdata('email'),'publish'=>1),'',array('date','DESC'));
            $email2 = $this->common_model->getdata('email',array('email_from'=>$this->session->userdata('email'),'publish'=>1),'',array('date','DESC'));
            if(!empty($dat['email2'])){
                foreach($dat['email2'] as $m){
                    $mang1[] = $m->email_from;
                }
            $mang2 = array_unique($mang1);
            foreach ($mang2 as $mang3){
                    $this->db->order_by('id','DESC');
                    $dat['email'][]=$this->common_model->getone('email',array('email_to'=>$this->session->userdata('email'),'email_from'=>$mang3,'publish'=>1));
                }
            $this->total_rows=count($dat['email']);
            }
            else{
                if(!empty($email2)){
                    foreach($email2 as $m){
                        $mang1[] = $m->email_to;
                    }
                $mang2 = array_unique($mang1);
                foreach ($mang2 as $mang3){
					$this->db->order_by('id','DESC');
					$dat['email'][]=$this->common_model->getone('email',array('email_from'=>$this->session->userdata('email'),'email_to'=>$mang3,'publish'=>1));
				}
                $this->total_rows=count($dat['email']);
                }
            }
            $this->phantrang();
            $data['content']=$this->load->view('mod_email/thuden',$dat,true);
       }else{
            $url = base_url().'email/chi-tiet/'.$offset.'.html';
            $data['content']=$this->load->view('mod_account/views_login',array('url'=>$url,'thongbao'=>$thongbao),true);
       }
       $this->load->view('index',$data); 
    }
	
    function thudi(){
        $offset=$this->uri->segment(3);
        $url=$this->uri->segment(1).'/'.$this->uri->segment(2).'.html';
       $thongbao='Bạn cần đăng nhập để sử dụng chức năng này';
       if($this->session->userdata('id')){    
           $dat['activedi']=1;
           $dat['email2']=$this->common_model->getdata('email',array('email_from'=>$this->session->userdata('email'),'publish'=>1),'',array('date','DESC'),array($this->per_page,$offset));
            foreach($dat['email2'] as $m)
                {
                    $mang1[] = $m->email_from;
                }
            $mang2 = array_unique($mang1);
            foreach ($mang2 as $mang3)
                {
                    $this->db->order_by('id','DESC');
                    $dat['email'][]=$this->common_model->getone('email',array('email_from'=>$this->session->userdata('email'),'email_to'=>$mang3));
                }
           $this->total_rows=$this->common_model->getnumber2('email',array('email_from'=>$this->session->userdata('email'),'publish'=>1));
           $this->phantrang();
           $data['content']=$this->load->view('mod_email/thudi',$dat,true);
       }
       else
       {
            $url = base_url().'email/chi-tiet/'.$offset.'.html';
            $data['content']=$this->load->view('mod_account/views_login',array('url'=>$url,'thongbao'=>$thongbao),true);
       }
       $this->load->view('index',$data); 
    }
    
     function chitiet(){
        $dat['activeden']=1;
        $id=$this->uri->segment(3);
        $url=$this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$id;
        $thongbao='Bạn cần đăng nhập để sử dụng chức năng này';
        if($this->session->userdata('id')){
            $dat['email2']=$this->common_model->getdata('email',array('publish'=>1,'email_to'=>$this->session->userdata('email')),'',array('date','DESC'));
			
            if(!empty($dat['email2']))
            {
                foreach($dat['email2'] as $m)
                {
                    $mang1[] = $m->email_from;
                }
                $mang2 = array_unique($mang1);
                foreach ($mang2 as $mang3)
                {
                    $this->db->order_by('id','DESC');
                    $dat['email'][]=$this->common_model->getone('email',array('publish'=>1,'email_to'=>$this->session->userdata('email'),'email_from'=>$mang3),'',array('date','DESC'),'',array('email_from'=>$this->session->userdata('email'),'email_to'=>$mang3));
                }
            }
            $this->total_rows=$this->common_model->getnumber2('email',array('publish'=>1,'email_to'=>$this->session->userdata('email')),'','','',array('email_from'=>$this->session->userdata('email')));
            $this->phantrang();
           

            $dat['email2']=$this->common_model->getone('email',array('id'=>$id,'publish'=>1));
            if($dat['email2']->email_to == $this->session->userdata('email') || $dat['email2']->email_from == $this->session->userdata('email'))
                {
                    $dat['emailgui']=$this->common_model->getdata('email',array('email_from'=>$dat['email2']->email_from,'email_to'=>$dat['email2']->email_to,'publish'=>1),'',array('id','DESC'));
                    $dat['emailnhan']=$this->common_model->getdata('email',array('email_from'=>$dat['email2']->email_to,'email_to'=>$dat['email2']->email_from,'publish'=>1),'',array('id','DESC'));
                    $dat['user']=$this->common_model->getone('users',array('email'=>$dat['email2']->email_from));
                    if(!empty($dat['emailnhan']))
                        {
                           $dat["mangnoi"] = array_merge($dat['emailgui'],$dat['emailnhan']); 
                        }
                    else
                        {
                            $dat["mangnoi"] = $dat['emailgui'];
                        }
                    arsort($dat["mangnoi"]);
                    //start set status cho email
                    $this->common_model->updatedata('email',array('email_from'=>$dat['email2']->email_from),array('status'=>1));
                    //end set status
                    if($this->input->post('mes_traloi') != ''){
                            $mess = $this->input->post('mes_traloi');
                            $mail_from = $this->session->userdata('email');
                            $mail_to = $dat['email2']->email_from;
                            $this->common_model->insertdata('email',array('email_from'=>$mail_from,'email_to'=>$mail_to,'subjects'=>$dat['email2']->subjects,'description'=>$mess));
                            $dat['thongbao']='<p style="color:green;"><i class="icon-check-sign icon-2x"></i>&nbsp;&nbsp;Bạn đã gửi email thành công. Ban quản trị sẽ duyệt trong vòng 24h</p>';
                        }
                    $data['content']=$this->load->view('mod_email/chitiet',$dat,true);
                }
            else
                {
                    $data['content']='<div class="wrapper clearfix min720"><h1>Bạn không có quyền xem email này!</h1></div>';
                }
        }else{
            $url = base_url().'email/chi-tiet/'.$id.'.html';
            $data['content']=$this->load->view('mod_account/views_login',array('url'=>$url,'thongbao'=>$thongbao),true);
       }
        $this->load->view('index',$data);
    }
    function chitietdi(){
        $dat['activedi']=1;
        $id=$this->uri->segment(3);
        $url=$this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$id;
        $thongbao='Bạn cần đăng nhập để sử dụng chức năng này';
        if($this->session->userdata('id')){
            $dat['email']=$this->common_model->getone('email',array('id'=>$id,'publish'=>1));
            $this->common_model->updatedata('email',array('id'=>$id),array('status'=>1));
            $data['content']=$this->load->view('mod_email/chitiet_thudi',$dat,true);
        }else{
            $data['content']=$this->load->view('mod_account/views_login',array('url'=>$url,'thongbao'=>$thongbao),true);
       }     
        $this->load->view('index',$data);
    }
     function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = base_url().$this->uri->segment(1).'/'.$this->uri->segment(2);
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 3;
        $config['num_links'] = 12;
        $this->pagination->initialize($config);
     }
}